/***********************************************************************
 * Module:  RandomCodeServlet.java
 * Author:  suwei
 * Purpose: Defines the Class RandomCodeServlet
 ***********************************************************************/

package com.sa.commons.function.randomCode;

import java.awt.Color;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sa.commons.function.randomCode.impl.RandomPicCreatorImpl;
import com.sa.commons.function.randomCode.impl.RandomValidatorImpl;
import com.sa.commons.function.randomCode.inter.RandomPicCreator;
import com.sa.commons.function.randomCode.inter.RandomValidator;


/**
 * 生成验证码的servlet
 */
public class RandomPicServlet extends HttpServlet {

	private static Log log = LogFactory.getLog(RandomPicServlet.class);
	
	private static final long serialVersionUID = 3796351198097771007L;

	private Color color = new Color(25, 60, 170);
	private int maxLen = 7;
	private int minLen = 5;
	private int width = 180;
	private int height = 50;

	private RandomPicCreator creator;
	private RandomValidator validator;

	@Override
	public void destroy() {
		creator = null;
		super.destroy();
	}

	@Override
	public void init(ServletConfig config) throws ServletException {
		validator = new RandomValidatorImpl();
		
		String colorStr = config.getInitParameter("color");
		String maxLenStr = config.getInitParameter("maxLen");
		String minLenStr = config.getInitParameter("minLen");
		String widthStr = config.getInitParameter("width");
		String heightStr = config.getInitParameter("height");

		try {
			if (colorStr != null) {
				String[] parts = colorStr.split(",");

				if (parts.length == 3) {
					int part0 = Integer.parseInt(parts[0]);
					int part1 = Integer.parseInt(parts[1]);
					int part2 = Integer.parseInt(parts[2]);
					color = new Color(part0, part1, part2);
				}
			}

		} catch (NumberFormatException e) {
			e.printStackTrace();
		}

		try {
			if (maxLenStr != null && minLenStr != null) {
				maxLen = Integer.parseInt(maxLenStr);
				minLen = Integer.parseInt(minLenStr);
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}

		try {
			if (widthStr != null && heightStr != null) {
				width = Integer.parseInt(widthStr);
				height = Integer.parseInt(heightStr);
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}

		creator = new RandomPicCreatorImpl(color, maxLen, minLen, width, height);
		super.init(config);
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("image/png");
		response.setHeader("cache", "no-cache");
		HttpSession session = request.getSession(true);
		OutputStream os = response.getOutputStream();
		String randomCode = creator.createPic(os);
		
		log.debug("产生随机码图片："+randomCode);
		validator.initRandom(randomCode, session);
		os.flush();
		os.close();
	}
}